package cn.ezios.huo.system.biz;

import cn.ezios.huo.common.mvc.Ret;
import cn.ezios.huo.common.utils.PasswordCall;
import cn.ezios.huo.system.dao.domain.SysUser;
import cn.ezios.huo.system.dao.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/sys_user")
@RequiredArgsConstructor
public class V1SysUserController {

    private final SysUserService sysUserService;

    @GetMapping("{id}")
    public Ret getUser(@PathVariable("id") String id) {
        SysUser user = sysUserService.getById(id);
        return Ret.ok(user);
    }

    /**
     * 添加用户
     */
    @PostMapping("add")
    public Ret addUser(@RequestBody SysUser user) {
        sysUserService.save(user);
        return Ret.ok();
    }
    /**
     * 注册用户
     */
    @PostMapping("register")
    public Ret registerUser(@RequestBody SysUser user) {
        String password = user.getPassword();
        /**
         * 加盐加密
         */
        String generatePassword = PasswordCall.generatePassword(password);
        user.setPassword(generatePassword);
        sysUserService.save(user);
        return Ret.ok();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("{id}")
    public Ret deleteUser(@PathVariable("id") String id) {
        sysUserService.removeById(id);
        return Ret.ok();
    }
}
